package com.css.zfzx.fwsjcj.modules.analysis.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.zfzx.fwsjcj.modules.analysis.service.AnalysisService;
import com.css.zfzx.fwsjcj.modules.analysis.vo.AnalysisQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author leon
 * @Title: AnalysisController
 * @Description: 普查统计分析
 * @date 2020/6/9 11:05
 */
@Slf4j
@RestController
@RequestMapping("fw/analysis")
public class AnalysisController {
    @Autowired
    private AnalysisService analysisService;


    /**
     * 柱状图-个数
     *
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("buildingNum")
    public RestResponse buildingNum(AnalysisQueryVo analysisQueryVo) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            Map<String, Object> data = analysisService.buildingNumData(analysisQueryVo);
            json.put("data", data);
            json.put("message", "查询成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 柱状图-面积
     *
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("buildingArea")
    public RestResponse buildingArea(AnalysisQueryVo analysisQueryVo) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            Map<String, Object> data = analysisService.buildingAreaData(analysisQueryVo);
            json.put("data", data);
            json.put("message", "查询成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 城市饼状图-结构类型-栋数和面积
     *
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("buildingCity")
    public RestResponse buildingCity(AnalysisQueryVo analysisQueryVo) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            // 城市建筑栋数和面积
            List<Map> cityNum = analysisService.buildingCityNumDataByStructureType(analysisQueryVo);
            List<Map> cityArea = analysisService.buildingCityAreaDataByStructureType(analysisQueryVo);
            json.put("cityNum", cityNum);
            json.put("cityArea", cityArea);
            json.put("message", "查询成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 农村饼状图-结构类型-栋数和面积
     *
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("buildingVillage")
    public RestResponse buildingVillage(AnalysisQueryVo analysisQueryVo) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            // 农村建筑栋数和面积
            List<Map> villageNum = analysisService.buildingVillageNumDataByStructureType(analysisQueryVo);
            List<Map> villageArea = analysisService.buildingVillageAreaDataByStructureType(analysisQueryVo);
            json.put("villageNum", villageNum);
            json.put("villageArea", villageArea);
            json.put("message", "查询成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }


    /**
     * 饼状图-建造年代-个数和面积
     *
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("buildingTime")
    public RestResponse buildingDataByBuildingTime(AnalysisQueryVo analysisQueryVo) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            // 建筑栋数和面积
            List<Map> num = analysisService.buildingNumDataByBuildingTime(analysisQueryVo);
            List<Map> area = analysisService.buildingAreaDataByBuildingTime(analysisQueryVo);
            json.put("num", num);
            json.put("area", area);
            json.put("message", "查询成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }


    /**
     * 饼状图-设防烈度-个数和面积
     *
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("fortificationIntensity")
    public RestResponse buildingDataByFortificationIntensity(AnalysisQueryVo analysisQueryVo) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            // 栋数和面积
            List<Map> num = analysisService.buildingNumDataByFortificationIntensity(analysisQueryVo);
            List<Map> area = analysisService.buildingAreaDataByFortificationIntensity(analysisQueryVo);
            json.put("num", num);
            json.put("area", area);
            json.put("message", "查询成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 饼状图-(结论）情况统计-个数和面积
     *
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("result")
    public RestResponse buildingDataByResult(AnalysisQueryVo analysisQueryVo) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            List<Map> num = analysisService.buildingNumDataByResult(analysisQueryVo);
            List<Map> area = analysisService.buildingAreaDataByResult(analysisQueryVo);
            json.put("num", num);
            json.put("area", area);
            json.put("message", "查询成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 饼状图-房屋使用情况-个数和面积
     *
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("houseUseSituation")
    public RestResponse buildingDataByHouseUseSituation(AnalysisQueryVo analysisQueryVo) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            List<Map> num = analysisService.buildingNumDataByHouseUseSituation(analysisQueryVo);
            List<Map> area = analysisService.buildingAreaDataByHouseUseSituation(analysisQueryVo);
            json.put("num", num);
            json.put("area", area);
            json.put("message", "查询成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 饼状图-农村居住房屋抗震构造措施-个数和面积
     *
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("houseMeasures")
    public RestResponse buildingDataByHouseMeasures(AnalysisQueryVo analysisQueryVo) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            List<Map> num = analysisService.buildingNumDataByHouseMeasures(analysisQueryVo);
            List<Map> area = analysisService.buildingAreaDataByHouseMeasures(analysisQueryVo);
            json.put("num", num);
            json.put("area", area);
            json.put("message", "查询成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 饼状图-安全隐患占比-个数
     *
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("buildingIsDamage")
    public RestResponse buildingDataByBuildingIsDamage(AnalysisQueryVo analysisQueryVo) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            List<Map> num = analysisService.buildingNumDataByBuildingIsDamage(analysisQueryVo);
            List<Map> area = analysisService.buildingAreaDataByBuildingIsDamage(analysisQueryVo);
            json.put("num", num);
            json.put("area", area);
            json.put("message", "查询成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

}
